package search_simpleHTMLlist.simpleTests;

import java.io.*;

import jointPart_1_2_HTMLlist.HTMLlist;
import jointPart_1_2_HTMLlist.Searcher;
import jxl.JXLException;


public class SearchCmd {
    public static void main(String[] args) throws IOException, JXLException {
        String name;

        // Check that a filename has been given as argument
        if (args.length != 1) {																			/*1*/
            System.out.println("Usage: java SearchCmd <datafile>");
            System.exit(1);
        }

        // Read the file and create the linked list
        HTMLlist l = Searcher.readHtmlList (args[0]);

        // Ask for a word to search
        BufferedReader inuser =
            new BufferedReader (new InputStreamReader (System.in));

        System.out.println ("Type a word or Hit return to exit.");
        boolean quit = false;
        String URLsFound = "";
        
             
        while (!quit) {																		  			/*2*/																
      
            name = inuser.readLine(); // Read a line from the terminal
            
        	
            
            
            
            if (name == null || name.length() == 0) {													/*3*/
                quit = true;
            }
            // Store the return value in 'URLsFound' and if this variable
            // is not empty, we have found one or more URLs where 'name'
            // exists, so print the result to the console window            
            else if (!((URLsFound = Searcher.existsWithURLs(l, name)).isEmpty()))						/*4*/
            {
                System.out.println("The word \"" + name + "\" has been found in the following URLs:");
                System.out.println(URLsFound);
            } else 
                System.out.println("The word \"" + name + "\" has not been found.");
            }
            
            
            
        }
      
    }
